import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/gzt/gztView.dart';
import 'package:flutter_ahj_app/app/gzt/gztViewModel.dart';
import 'package:flutter_ahj_app/app/jc/JCHomeView.dart';
import 'package:flutter_ahj_app/app/jc/JCViewModel.dart';
import 'package:flutter_ahj_app/app/qingning/home/QNHomePage.dart';
import 'package:flutter_ahj_app/app/qingning/home/QNViewModel.dart';
import 'package:flutter_ahj_app/app/qingning/message/MessafeView.dart';
import 'package:flutter_ahj_app/app/sh/SHHomeView.dart';
import 'package:flutter_ahj_app/base/basewidget/viewmodel/ViewModelProvider.dart';
import 'package:flutter_ahj_app/base/cunchu/Constant.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:umeng_common_sdk/umeng_common_sdk.dart';
import 'base/cunchu/UserInfoUtils.dart';

class RootScene extends StatefulWidget {
  final int tabIndex;

  const RootScene({Key key, this.tabIndex}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return RootSceneState();
  }
}

class RootSceneState extends State<RootScene>
    with AutomaticKeepAliveClientMixin {
  var _scaffoldkey = new GlobalKey<ScaffoldState>();
  int _tabIndex = 0;
  List<Icon> _tabImages;
  List<Icon> _tabSelectedImages;
  String idType = Constant.USER_TYPE_DEDAULT;
  @override
  void initState() {
    super.initState();
    idType = UserInfoUtils.getUserType();
    UmengCommonSdk.initCommon('63340b2c7d111470b13d9f86', '63bf6bded64e6861391563c8', 'Umeng');

    // idType = Constant.USER_RISK_MANAG;//先强制设置成风险管理师
    _tabIndex = widget.tabIndex ?? 0;
    _tabImages = [
      Icon(IconData(0xe712, fontFamily: "iconfont")),
      Icon(IconData(0xe713, fontFamily: "iconfont")),
      // Icon(IconData(0xe711, fontFamily: "iconfont")),
      // Icon(IconData(0xe710, fontFamily: "iconfont")),
      // Icon(IconData(0xe70f, fontFamily: "iconfont")),

    ];


    _tabSelectedImages = [
      Icon(IconData(0xe715, fontFamily: "iconfont"), ),
      Icon(IconData(0xe714, fontFamily: "iconfont"),),
      // Icon(IconData(0xe716, fontFamily: "iconfont"),),
      // Icon(IconData(0xe717, fontFamily: "iconfont"),),
      // Icon(IconData(0xe718, fontFamily: "iconfont"),),

    ];

    Future.delayed(Duration.zero, () {
      UserInfoUtils.getInstance().then((b) {
        if (UserInfoUtils.getUserToken() != '' &&
            UserInfoUtils.getPW() != null &&
            UserInfoUtils.getPW() == "1") {
          // 判断是否是旧密码
          _showDialog(context);
          UserInfoUtils.setPW("0");
        }
      });
    });
  }

  // 提示框
  _showDialog(BuildContext context) {
    StatefulBuilder sb = StatefulBuilder(
      builder: (context, mSetState) {
        return new AlertDialog(
          title: new Text("温馨提示"),
          content: new Text("您的密码还是初始密码是否现在进行修改？"),
          actions: <Widget>[
            FlatButton(
                child: Text("是"),
                onPressed: () {
                  Navigator.pop(context);
                  Navigator.pushNamed(context, "/ChangeDefaultPWD");
                }),
            FlatButton(
                child: new Text("否"),
                onPressed: () {
                  Navigator.of(context).pop();
                })
          ],
        );
      },
    );

    showDialog(
        context: context, barrierDismissible: false, builder: (context) => sb);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldkey,
      body: WillPopScope(
          child: Scaffold(
        body: IndexedStack(
          index: _tabIndex,
          children: <Widget>[
            ViewModelProvider(viewModel: QNViewModel(), child: QNHomePage()),
            ViewModelProvider(viewModel: QNViewModel(), child: MessafeView()),
            // ViewModelProvider(viewModel: TestViewModel(), child: MessageView()),
            // ViewModelProvider(viewModel: TestViewModel(), child: TjPageView()),
            // ViewModelProvider(viewModel: AccountViewModel(), child: MyNewView()),
          ],
        ),
        bottomNavigationBar: CupertinoTabBar(
          iconSize: 20, // 图标大小
          backgroundColor: Colors.white,
          inactiveColor: Color(0XFF333333),
          activeColor: SQColor.theme_color,
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(icon: getTabIcon(0), label:'检查',),
            BottomNavigationBarItem(icon: getTabIcon(1), label:'审核',),
            // BottomNavigationBarItem(icon: getTabIcon(2), label: '消息', ),
            // BottomNavigationBarItem(icon: getTabIcon(3), label: '统计',),
            // BottomNavigationBarItem(icon: getTabIcon(4), label: '我的',),
          ],
          currentIndex: _tabIndex,
          onTap: (index) {
            setState(() {
              _tabIndex = index;
            });
          },
        ),
      )),
    );
  }

  Icon getTabIcon(int index) {
    if (index == _tabIndex) {
      return _tabSelectedImages[index];
    } else {
      return _tabImages[index];
    }
  }

  Icon getTabIcon1(int index) {
    if (index == _tabIndex) {
      return _tabImages[index];
    } else {
      return _tabSelectedImages[index];
    }
  }

  void onPageChanged(int page) {
    if (this._tabIndex == page) return;
    setState(() {
      this._tabIndex = page;
    });
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
